package com.wxmit.module.system.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wxmit.base.domain.ApiResult;
import com.wxmit.framework.excel.util.ExcelUtils;
import com.wxmit.framework.web.annotation.NoResponseBody;
import com.wxmit.framework.web.operatelog.Log;
import com.wxmit.framework.web.operatelog.LogModule;
import com.wxmit.framework.web.operatelog.enums.BusinessType;
import com.wxmit.module.system.domain.SysUser;
import com.wxmit.module.system.service.ISysUserService;
import com.wxmit.system.base.core.ShushanBaseController;
import com.wxmit.system.base.core.ShushanPage;
import com.wxmit.system.base.domain.PageDTO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 *  用户管理
 * @Author huangxiong
 * @Date 2022/7/19 16:27
 * @Version V1.0
 */
@RestController
@RequestMapping("/sys/user")
@LogModule("用户管理")
public class SysUserController extends ShushanBaseController<ISysUserService,SysUser,Long> {

    public SysUserController(ISysUserService sysUserService){
        super(sysUserService,"system:user:");
    }

    @Override
    @GetMapping("/{id:\\d+}")
    public ApiResult<SysUser> get(@PathVariable Long id){
        return ApiResult.successWithData(baseService.getInfo(id));
    }

    @PutMapping("/resetPw/{id}")
    @Log(module="用户管理",name="重置密码",type = BusinessType.UPDATE)
    public ApiResult resetPw(@PathVariable("id") Long id){
        return ApiResult.successWithData(baseService.resetPw(id));
    }

    @PutMapping("/status/{id}/{status}")
    @Log(module="用户管理",name="更新用户状态",type = BusinessType.UPDATE)
    public ApiResult updateStatus(@PathVariable("id") Long id,@PathVariable("status") String status){
        return ApiResult.successWithData(baseService.updateStatus(id,status));
    }

    @PutMapping("/unban/{id}")
    @Log(module="用户管理",name="解禁用户",type = BusinessType.UPDATE)
    public ApiResult unbanUser(@PathVariable("id") Long id){
        baseService.clearUpLoginErr(id);
        return ApiResult.success();
    }


    @PostMapping("export")
    @NoResponseBody
    public void export(HttpServletResponse response,@RequestBody SysUser searchData) throws IOException {
        IPage<SysUser> iPage = baseService.listPage(new PageDTO<SysUser>(ShushanPage.allPage(), searchData));
        List<SysUser> records = iPage.getRecords();
        responseExcel(response,"User",SysUser.class,records);
    }

    @GetMapping("export/template")
    @NoResponseBody
    public void exportTemplate(HttpServletResponse response) throws IOException {
        String fileName = "UserTemplate";
        setUpExportExcelResponse(response,fileName);
        ExcelUtils.exportEmptyTemplate(response.getOutputStream(),SysUser.class);
    }

    @PostMapping("import")
    public ApiResult importByTemplate(MultipartFile file) throws IOException {
        baseService.importExcel(file.getInputStream());
       return ApiResult.success();

    }
}
